package com.beetlestech.nfe.main.biz.com.impl;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.main.biz.com.HbLesson;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.constant.SQLID;
import com.beetlestech.nfe.main.db.tbl.CoursesectionDTO;
import com.beetlestech.nfe.main.db.tbl.HomeworkhistoryDTO;
import com.beetlestech.nfe.main.db.tbl.MstcourseDTO;
import com.beetlestech.nfe.main.db.tbl.MsthomeworkDTO;
import com.beetlestech.nfe.main.db.tbl.MstlessonDTO;
import com.beetlestech.nfe.main.db.tbl.SectionlessonDTO;
import com.beetlestech.nfe.main.db.tbl.StudenthomeworkDTO;
import com.beetlestech.nfe.main.db.tbl.StudyhistoryDTO;
import com.beetlestech.nfe.main.db.tbl.UsermessageDTO;

public class HbLessonImpl extends AbstractDBLogic implements HbLesson, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String SH_INFO = "课程表";

	public void openNextLesson(Integer orderID, Integer courseID, Integer sectionID, Integer lessonID, Integer studentID){

		// 查找下一课
		SectionlessonDTO nextLesson = getNextLesson(sectionID, lessonID);
		
		// 更新当前课程结束时间
		//20150320-qindong
		//updateEndDateTime(orderID, courseID, lessonID);
		
		// 存在下一课
		//if(nextLesson != null){
			// 更新下一课可用状态
			//updateNextLesson(orderID, courseID, nextLesson.getlessonid());
		//}
		
		// 查找当前课程作业ID
		Integer homeWorkID = getHomeWorkID(lessonID);
		
		if(homeWorkID != null){
			StudenthomeworkDTO studenthomeworkDto=getStudentHomework(studentID, homeWorkID);
			if(studenthomeworkDto==null){
				// 创建作业
				createStudentHomeWork(studentID, homeWorkID);
				
				// 获得文件ID
				Integer homeWorkFileID = getHomeWorkFileID(homeWorkID);
	
				// 创建作业履历
				createHomeWorkHistory(homeWorkID, studentID, homeWorkFileID);
				
				// 获得课程名
				String courseName = getCourseName(courseID);
	
				// 获得单元名
				String sectionName = getSectionName(courseID, sectionID);
				
				// 获得小节名
				String LessonName = getLessonName(lessonID);
				
				// 创建信息
				String titleString = "[" + courseName + "][" + sectionName + "][" + LessonName + "]";
				createMessage(titleString, studentID);
			}
		}
	}

	public void updateFirstWatchLesson(Integer orderID, Integer courseID, Integer lessonID){
		StudyhistoryDTO studyHistory = new StudyhistoryDTO();
		studyHistory.setorderid(orderID);
		studyHistory.setcourseid(courseID);
		studyHistory.setlessonid(lessonID);
		studyHistory.setstartdate(getCurrentDate());
		studyHistory.setstarttime(getCurrentTime());
		studyHistory.setisfirstwatch(Integer.parseInt(VIDEO_STATE.FIRSTWATCH.toString()));
		studyHistory.setlastdate(getCurrentDate());
		studyHistory.setlasttime(getCurrentTime());
		updateDAO.updateOne(StudyhistoryDTO.UPDATE, studyHistory, M10190WS, SH_INFO);
	}
	
	public void updateWatchTime(Integer orderID, Integer courseID, Integer lessonID, String watchTime){
		StudyhistoryDTO studyHistory = new StudyhistoryDTO();
		studyHistory.setorderid(orderID);
		studyHistory.setcourseid(courseID);
		studyHistory.setlessonid(lessonID);
		studyHistory.setwatchtime(watchTime);
		studyHistory.setisfirstwatch(Integer.parseInt(VIDEO_STATE.NOTFIRSTWATCH.toString()));
		studyHistory.setlastdate(getCurrentDate());
		studyHistory.setlasttime(getCurrentTime());
		updateDAO.updateOne(StudyhistoryDTO.UPDATE, studyHistory, M10190WS, SH_INFO);
	}

	private SectionlessonDTO getNextLesson(Integer sectionID, Integer lessonID){
		StatementDTO statementDTO = new StatementDTO();
		//20150322-qindong
		//StatementUtil.andWhere(statementDTO, SectionlessonDTO.COLUMN_SECTION_ID, StatementUtil.KEYWORD.GREATER_EQUAL_THAN, sectionID);
		StatementUtil.andWhere(statementDTO, SectionlessonDTO.COLUMN_LESSON_ID, StatementUtil.KEYWORD.GREATER_THAN, lessonID);
		//StatementUtil.andWhere(statementDTO, "rowNo", StatementUtil.KEYWORD.EQUAL, 1);
		
		// 查找下一课
		SectionlessonDTO searchDto = new SectionlessonDTO();
		searchDto.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		return queryDAO.queryForObject(SQLID.NEXT_LESSON_FIND, searchDto);
	}
	
	private StudenthomeworkDTO getStudentHomework(Integer userid, Integer homeworkid){
		StatementDTO statementDTO = new StatementDTO();
		StatementUtil.andWhere(statementDTO, StudenthomeworkDTO.COLUMN_USR_ID, StatementUtil.KEYWORD.EQUAL, userid);
		StatementUtil.andWhere(statementDTO, StudenthomeworkDTO.COLUMN_HOMEWORK_ID, StatementUtil.KEYWORD.EQUAL, homeworkid);
		//StatementUtil.andWhere(statementDTO, "rowNo", StatementUtil.KEYWORD.EQUAL, 1);
		
		// 查找下一课
		StudenthomeworkDTO searchDto = new StudenthomeworkDTO();
		searchDto.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		return queryDAO.queryForObject(SQLID.COURSE_STUDENT_HOMEWORK_SEARCH, searchDto);
	}

	private void updateEndDateTime(Integer orderID, Integer courseID, Integer lessonID){
		// 更新当前课程结束时间
		StudyhistoryDTO studyHistory = new StudyhistoryDTO();
		studyHistory.setorderid(orderID);
		studyHistory.setcourseid(courseID);
		studyHistory.setlessonid(lessonID);
		studyHistory.setenddate(getCurrentDate());
		studyHistory.setendtime(getCurrentTime());
		studyHistory.setlastdate(getCurrentDate());
		studyHistory.setlasttime(getCurrentTime());
		updateDAO.updateOne(StudyhistoryDTO.UPDATE, studyHistory, M10190WS, SH_INFO);
	}
	
	private void updateNextLesson(Integer orderID, Integer courseID, Integer lessonID){
		// 更新下一课可用状态
		StudyhistoryDTO studyHistory = new StudyhistoryDTO();
		studyHistory.setorderid(orderID);
		studyHistory.setcourseid(courseID);
		studyHistory.setlessonid(lessonID);
		studyHistory.setstate(Integer.parseInt(CATAGORY_P_LESSON.ENABLE.toString()));
		studyHistory.setlastdate(getCurrentDate());
		studyHistory.setlasttime(getCurrentTime());
		updateDAO.updateOne(StudyhistoryDTO.UPDATE, studyHistory, M10190WS, SH_INFO);
	}

	private Integer getHomeWorkID(Integer lessonID){
		// 查找当前课程作业ID
		MstlessonDTO searchDTO = new MstlessonDTO();
		searchDTO.setlessonid(lessonID);
		MstlessonDTO lessonDTO = queryDAO.queryForObject(MstlessonDTO.EQUAL_SEARCH, searchDTO);
		if(lessonDTO == null){
			return null;
		}
		else{
			return lessonDTO.gethomeworkid();
		}
	}

	private void createStudentHomeWork(Integer userID, Integer homeWorkID){
		StudenthomeworkDTO studentHomework = new StudenthomeworkDTO();
		studentHomework.setusrid(userID);
		studentHomework.sethomeworkid(homeWorkID);
		studentHomework.setsenderid(SERVER_ID);
		studentHomework.setsenddate(getCurrentDate());
		studentHomework.setsendtime(getCurrentTime());
		studentHomework.setstate(Integer.parseInt(CATAGORY_P_HOMEWORK_STATE.UNFINISHED.toString()));

		updateDAO.updateOne(StudenthomeworkDTO.INSERT, studentHomework, M10190WS, SH_INFO);
	}

	private Integer getHomeWorkFileID(Integer homeWorkID){
		MsthomeworkDTO searchDTO = new MsthomeworkDTO();
		searchDTO.sethomeworkid(homeWorkID);
		MsthomeworkDTO homeWork = queryDAO.queryForObject(MsthomeworkDTO.EQUAL_SEARCH, searchDTO);
		return homeWork.getfileid();
	}
	
	private void createHomeWorkHistory(Integer homeWorkID, Integer studentID, Integer homeWorkFileID){
		// 创建作业履历
		HomeworkhistoryDTO homeworkHistory = new HomeworkhistoryDTO();
		homeworkHistory.sethomeworkid(homeWorkID);
		homeworkHistory.setsenderid(SERVER_ID);
		homeworkHistory.setreceiverid(studentID);
		homeworkHistory.setfileid(homeWorkFileID);
		homeworkHistory.setstate(0);
		updateDAO.updateOne(HomeworkhistoryDTO.INSERT, homeworkHistory, M10190WS, SH_INFO);
	}

	private String getCourseName(Integer courseID){
		MstcourseDTO searchDTO = new MstcourseDTO();
		searchDTO.setcourseid(courseID);
		MstcourseDTO course = queryDAO.queryForObject(MstcourseDTO.EQUAL_SEARCH, searchDTO);
		return course.getname();
	}
	
	private String getSectionName(Integer courseID, Integer sectionID){
		CoursesectionDTO searchDTO = new CoursesectionDTO();
		searchDTO.setcourseid(courseID);
		searchDTO.setsectionid(sectionID);
		CoursesectionDTO coursesection = queryDAO.queryForObject(CoursesectionDTO.EQUAL_SEARCH, searchDTO);
		return coursesection.getname();
	}
	
	private String getLessonName(Integer lessonID){
		MstlessonDTO searchDTO = new MstlessonDTO();
		searchDTO.setlessonid(lessonID);
		MstlessonDTO lesson = queryDAO.queryForObject(MstlessonDTO.EQUAL_SEARCH, searchDTO);
		return lesson.getname();
	}
	
	private void createMessage(String titleString, Integer studentID){
		// 创建消息
		UsermessageDTO userMessage = new UsermessageDTO();
		userMessage.setTitel(titleString + "随堂作业");
		userMessage.setMessage("您已完成" + titleString + "的学习,系统为您发送该节随堂作业!");
		userMessage.setSenderId(SERVER_ID);
		userMessage.setReceiveId(studentID);
		userMessage.setSendDT(getCurrentDate());
		userMessage.setSendTime(getCurrentTime());
		userMessage.setReadflg(Integer.parseInt(CATAGORY_P_MESSAGE_STATE.UNREAD.toString()));
		updateDAO.updateOne(UsermessageDTO.INSERT, userMessage, M10190WS, SH_INFO);
	}
}
